package cn.machen.www.fashion.utils;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 功能：发送邮箱
 */
public class MailUtils {

   public static void sendMail(String email,String msg) throws AddressException {
       //创建程序与邮件服务器的session
       Properties props = new Properties();
       props.setProperty("mail.transport.protocol","SMTP");  //设置邮件协议
       props.setProperty("mail.host","smtp.163.com"); //设置邮件服务器
       props.setProperty("mail.smtp.auth","true");  //指定验证为ture
       //创建验证器
       Authenticator authenticator = new Authenticator(){
           public PasswordAuthentication getPasswordAuthentication(){
             return new PasswordAuthentication("enjoy_life_mc","mc960702mc");
           };
       };
       Session session = Session.getInstance(props, authenticator);
       
        //创建message，设置邮箱内容
       Message message = new MimeMessage(session);
       try {
           message.setFrom(new InternetAddress("enjoy_life_mc@163.com"));//设置发件人
           message.setRecipient(Message.RecipientType.TO,new InternetAddress(email));   //设置发送方式与接收者
            message.setSubject("用户激活"); //设置邮件主题
           message.setContent(msg,"text/html;charset=utf-8");

           //发送邮箱
           Transport.send(message);
       } catch (MessagingException e) {
           e.printStackTrace();
       }
   }


}
